const joi = require('joi');
const dbQuery = require('../model/db');
const {
	sendError,
	sendSuccess
} = require('../utils/sendFunction');

// 验证规则
// name sex picfile introduction singerTypeId
const schemaAddSinger = joi.object({
	name: joi.string().required().error(new Error('name不符合验证规则')),
	sex: joi.string().required().error(new Error('sex不符合验证规则')),
	introduction: joi.string().required().error(new Error('introdution不符合验证规则')),
	singerTypeId: joi.number().required().error(new Error('singerTypeId不符合验证规则'))
})

const schemaGetSingerAndDelete = joi.object({
	id: joi.number().required().error(new Error('id不符合验证规则')),
})

const schemaGetSingerTypeAndDelete = joi.object({
	id: joi.number().required().error(new Error('id不符合验证规则')),
})

const schemaAddSingerType = joi.object({
	name: joi.string().required().error(new Error('name不符合验证规则')),
	create_time: joi.string().required().error(new Error('create_time不符合验证规则')),
})

const schemaEditSingerType = joi.object({
	name: joi.string().alphanum().required().error(new Error('name不符合验证规则')),
	id: joi.number().required().error(new Error('id不符合验证规则'))
})

const schemaEditSinger = joi.object({
	name: joi.string().required().error(new Error('name不符合验证规则')),
	sex: joi.string().required().error(new Error('sex不符合验证规则')),
	introduction: joi.string().required().error(new Error('introdution不符合验证规则')),
	id: joi.number().required().error(new Error('id不符合验证规则')),
	singerTypeId: joi.number().required().error(new Error('singerTypeId不符合验证规则'))
})

// name sex picfile introduction singerTypeId
const addSinger = async function (req, res, next) {
	let info = req.body;

	if (!info.name || !info.sex || !info.introduction || !info.singerTypeId) {
		sendError(res, 200, '歌手名称或性别或歌手介绍或歌手类型不能为空!');
		return;
	}

	if (!req.file) {
		sendError(res, 200, '图片不能为空！')
		return
	}

	try {
		await schemaAddSinger.validateAsync({
			name: info.name,
			sex: info.sex,
			introduction: info.introduction,
			singerTypeId: info.singerTypeId
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singer where singer_name=?';
	let result = await dbQuery(sqlStr, info.name);
	if (result.length !== 0) {
		sendError(res, 200, '歌手名称被占用，请更换其他歌手名称!');
		return;
	}

	let pic = req.file.path.split('server\\')[1].replace('\\', '/').replace('\\', '/');
	let sqlStr1 = 'insert into singer (singer_name,singer_sex,singer_pic_url,singer_introduction,singertype_id) values (?,?,?,?,?)';
	let result1 = await dbQuery(sqlStr1, [info.name, info.sex, pic, info.introduction, info.singerTypeId]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '添加歌手成功!');
	} else {
		sendError(res, 500, '添加歌手失败，请稍后再试!')
	}
}

const deleteSinger = async function (req, res, next) {
	let {
		id
	} = req.query;
	if (!id) {
		sendError(res, 200, 'id不能为空!');
		return;
	}

	try {
		await schemaGetSingerAndDelete.validateAsync({
			id
		})
	} catch (error) {
		next(error);
		next;
	}

	// 级联删除
	let sqlStr = 'delete from singer where singerid=?';
	let result = await dbQuery(sqlStr, [id]);
	let sqlStr1 = 'delete from music where singer_id=?';
	let result1 = await dbQuery(sqlStr1, [id]);
	let sqlStr2 = 'delete from mv where singer_id=?';
	let result2 = await dbQuery(sqlStr2, [id]);
	if (result.affectedRows == 1 && result1.affectedRows >= 0 && result2.affectedRows >= 0) {
		sendSuccess(res, '删除歌手成功!');
	} else {
		sendError(res, 500, '删除歌手失败，请稍后再试!')
	}
}

const getSinger = async function (req, res, next) {
	let sqlStr = 'select singer.*,singertype.* from singer inner join singertype on singer.singertype_id=singertype.singertypeid';
	let result = await dbQuery(sqlStr);
	if (result.length >= 0) {
		sendSuccess(res, '获取歌手成功!', {
			data: result
		})
	} else {
		sendError(res, 500, '获取歌手失败，请稍后再试!')
	}
}

// id name sex picfile introduction singerTypeId
const editSinger = async function (req, res, next) {
	let info = req.body;

	if (!info.name || !info.sex || !info.introduction || !info.id || !info.singerTypeId) {
		sendError(res, 200, '歌手名称或歌手性别或歌手介绍或id或歌手类型不能为空!');
		return;
	}

	try {
		await schemaEditSinger.validateAsync({
			name: info.name,
			sex: info.sex,
			introduction: info.introduction,
			id: info.id,
			singerTypeId: info.singerTypeId
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singer where singer_name=? and singerid!=?';
	let result = await dbQuery(sqlStr, [info.name, info.id]);
	if (result.length !== 0) {
		sendError(res, 200, '歌手名称被占用，请更换其他歌手名称!');
		return;
	}

	if (req.file) {
		let pic = req.file.path.split('server\\')[1].replace('\\', '/').replace('\\', '/');
		let sqlStr1 = 'update singer set singer_name=?,singer_sex=?,singer_pic_url=?,singer_introduction=?,singertype_id=? where singerid=?'
		let result1 = await dbQuery(sqlStr1, [info.name, info.sex, pic, info.introduction, info.singerTypeId, info.id]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '修改歌手成功!');
		} else {
			sendError(res, 500, '修改歌手失败，请稍后再试!');
		}
	} else {
		let sqlStr1 = 'update singer set singer_name=?,singer_sex=?,singer_introduction=?,singertype_id=? where singerid=?'
		let result1 = await dbQuery(sqlStr1, [info.name, info.sex, info.introduction, info.singerTypeId, info.id]);
		if (result1.affectedRows == 1) {
			sendSuccess(res, '修改歌手成功!');
		} else {
			sendError(res, 500, '修改歌手失败，请稍后再试!');
		}
	}
}

const getSingerById = async function (req, res, next) {
	let {
		id
	} = req.query;

	if (!id) {
		sendError(res, 200, 'id不能为空！');
		return;
	}

	try {
		await schemaGetSingerAndDelete.validateAsync({
			id
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singer where singerid=?';
	let result = await dbQuery(sqlStr, [id]);
	if (result.length >= 0) {
		sendSuccess(res, '获取歌手成功!', {
			data: result
		})
	} else {
		sendError(res, 500, '获取歌手失败，请稍后再试!')
	}
}

// singertypeid
const getSingerBySingerTypeId = async function (req, res, next) {
	let {
		singertypeid
	} = req.query;

	if (!singertypeid) {
		sendError(res, 200, '歌手类型不能为空！');
		return;
	}

	try {
		await schemaGetSingerAndDelete.validateAsync({
			id: singertypeid
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select singer.*,singertype.* from singer inner join singertype on singer.singertype_id=singertype.singertypeid where singertype_id=?';
	let result = await dbQuery(sqlStr, [singertypeid]);
	if (result.length >= 0) {
		sendSuccess(res, '获取歌手成功!', {
			data: result
		})
	} else {
		sendError(res, 500, '获取歌手失败，请稍后再试!')
	}
}

// name create_time
const addSingerType = async function (req, res, next) {
	let {
		name,
		create_time
	} = req.body;

	if (!name || !create_time) {
		sendError(res, 200, '歌手类型名称或创建时间不能为空!');
		return;
	}

	try {
		await schemaAddSingerType.validateAsync({
			name,
			create_time
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singertype where singertype_name=?'
	let result = await dbQuery(sqlStr, name);
	if (result.length !== 0) {
		sendError(res, 200, '歌手类型名称被占用，请更换其他歌手类型名称!');
		return;
	}

	let sqlStr1 = 'insert into singertype (singertype_name,singertype_create_time) values (?,?)';
	let result1 = await dbQuery(sqlStr1, [name, create_time]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '添加歌手类型成功!');
	} else {
		sendError(res, 500, '添加歌手类型失败，请稍后再试!')
	}
}

// id name(new)
const editSingerType = async function (req, res, next) {
	let {
		id,
		name
	} = req.body;

	if (!name || !id) {
		sendError(res, 200, '歌手类型名称或id不能为空!');
		return;
	}

	try {
		await schemaEditSingerType.validateAsync({
			name,
			id
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singertype where singertype_name=? and singertypeid!=?'
	let result = await dbQuery(sqlStr, [name, id]);
	if (result.length !== 0) {
		sendError(res, 200, '歌手类型名称已被占用，请更换后重试!');
		return;
	}

	let sqlStr1 = 'update singertype set singertype_name=? where singertypeid=?'
	let result1 = await dbQuery(sqlStr1, [name, id]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '修改歌手类型成功!');
	} else {
		sendError(res, 500, '修改歌手类型失败，请稍后再试!')
	}
}

const deleteSingerType = async function (req, res, next) {
	let {
		id
	} = req.query;

	if (!id) {
		sendError(res, 200, 'id不能为空!');
		return;
	}

	try {
		await schemaGetSingerTypeAndDelete.validateAsync({
			id
		})
	} catch (error) {
		next(error);
		return;
	}

	// 如果singer中有该singertype,就不能删除
	let sqlStr = 'select * from singer where singertype_id=?';
	let result = await dbQuery(sqlStr, [id]);
	if (result.length !== 0) {
		sendError(res, 200, '删除歌手类型失败，该歌手类型有对应的歌手!')
		return;
	}

	// 删除
	let sqlStr1 = 'delete from singertype where singertypeid=?';
	let result1 = await dbQuery(sqlStr1, [id]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '删除歌手类型成功!');
	} else {
		sendError(res, 500, '删除歌手类型失败，请稍后再试!')
	}
}

const getSingerType = async function (req, res, next) {
	let sqlStr = 'select * from singertype';
	let result = await dbQuery(sqlStr);
	if (result.length >= 0) {
		sendSuccess(res, '获取歌手类型成功!', {
			data: result
		})
	} else {
		sendError(res, 500, '获取歌手类型失败，请稍后再试!')
	}
}

const getSingerTypeById = async function (req, res, next) {
	let {
		id
	} = req.query;

	if (!id) {
		sendError(res, 200, 'id不能为空！');
		return
	}

	try {
		await schemaGetSingerTypeAndDelete.validateAsync({
			id
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from singertype where singertypeid=?';
	let result = await dbQuery(sqlStr, [id]);
	if (result.length >= 0) {
		sendSuccess(res, '获取歌手类型成功!', {
			data: result
		})
	} else {
		sendError(res, 500, '获取歌手类型失败，请稍后再试!')
	}
}


module.exports = {
	getSingerTypeById,
	addSinger,
	deleteSinger,
	getSinger,
	editSinger,
	addSingerType,
	editSingerType,
	deleteSingerType,
	getSingerType,
	getSingerById,
	getSingerBySingerTypeId
}